﻿// SetUpStusDlg.cpp : 実装ファイル
//

#include "stdafx.h"
#include "LookbackAdapter.h"
#include "SetUpStusDlg.h"
#include "afxdialogex.h"
#include "CommLib.h"
#include "SetUpInfoDlg.h"
#include "Resource.h"

// SetUpStusDlg ダイアログ

CWinThread* pThread;

#define IPSET_EVENT_NAME _T("IPSET_EVENT_NAME")

IMPLEMENT_DYNAMIC(SetUpStusDlg, CDialogEx)

SetUpStusDlg::SetUpStusDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(SetUpStusDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

SetUpStusDlg::~SetUpStusDlg()
{
	//if(m_nTimer){
	//	KillTimer(m_nTimer);
	//}
}

void SetUpStusDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	m_Progress.SetRange(1,100);
	m_Progress.SetStep(1);
	DDX_Control(pDX, IDOK, m_return);
	DDX_Control(pDX, IDOK2, m_install);
}


BEGIN_MESSAGE_MAP(SetUpStusDlg, CDialogEx)
	ON_BN_CLICKED(IDOK2, &SetUpStusDlg::OnBnClickedOk2)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDCANCEL, &SetUpStusDlg::OnBnClickedCancel)
END_MESSAGE_MAP()

INT_PTR SetUpStusDlg::DoModal(LANGID language, LANGID subLanguage)
{
	Language = language;
	SubLanguage = subLanguage;

	IpAddrArray = nullptr;
	return CDialogEx::DoModal();
}

INT_PTR SetUpStusDlg::DoModal(LANGID language, LANGID subLanguage,CStringArray *ipAddrArray)
{
	Language = language;
	SubLanguage = subLanguage;
	IpAddrArray = ipAddrArray;
	return CDialogEx::DoModal();
}

// CLookbackAdapterDlg メッセージ ハンドラー


BOOL SetUpStusDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// このダイアログのアイコンを設定します。アプリケーションのメイン ウィンドウがダイアログでない場合、
	//  Framework は、この設定を自動的に行います。
	SetIcon(m_hIcon, TRUE);			// 大きいアイコンの設定
	SetIcon(m_hIcon, FALSE);		// 小さいアイコンの設定

	CString strmsg;
	strmsg.LoadString(IDS_STRING_Cancel);
	SetDlgItemText(IDCANCEL,strmsg);

	strmsg.LoadString(IDS_STRING_PRE);
	SetDlgItemText(IDOK,strmsg);
	
	strmsg.LoadString(IDS_STRING_Install);
	SetDlgItemText(IDOK2,strmsg);

	strmsg.LoadString(IDS_STRING_INSTALL_INFO);
	SetDlgItemText(IDC_STATIC,strmsg);

	m_Progress.ShowWindow(FALSE);
	return TRUE;  // フォーカスをコントロールに設定した場合を除き、TRUE を返します。
}
// SetUpStusDlg メッセージ ハンドラー
void SetUpStusDlg::OnTimer(UINT nIDEvent) 
{
	 if(nIDEvent  == 1 && !IsContinue)
	 {
		m_Progress.StepIt();
	 }
	CDialog::OnTimer(nIDEvent);
}

unsigned __stdcall installDiver(void *pvParam)
{  
	CString strMsg;
	HANDLE eventhd;        
	_sleep(100);    

	LPCTSTR BaseName = TEXT("BaseName"); 
	LPCTSTR Machine = nullptr;
	DWORD Flags =1;
	int argc = 2;
	TCHAR* argv[] ={TEXT("C:\\Windows\\Inf\\Netloop.inf"),TEXT("*MSLOOP")};
	int ret = InstallLkbkAdpt(BaseName,Machine,Flags,argc,argv);
	if(ret == EXIT_REBOOT)
	{
		goto err;
	}
	else if(ret == EXIT_FAIL)
	{
		goto err;
	}
	((SetUpStusDlg*)pvParam)->m_Progress.SetPos(50);

	eventhd = OpenEvent(EVENT_ALL_ACCESS, FALSE, IPSET_EVENT_NAME);    
	SetEvent(eventhd);    
	CloseHandle(eventhd);
	return 0;

err:;
	((SetUpStusDlg*)pvParam)->m_return.EnableWindow(TRUE);
	((SetUpStusDlg*)pvParam)->m_install.EnableWindow(TRUE);

	strMsg.LoadString(IDS_STRING_Sucees);
	SetUpInfoDlg infoDlg;
	infoDlg.DoModal(strMsg);

	/* イベントをシグナル状態にする */    
	eventhd = OpenEvent(EVENT_ALL_ACCESS, FALSE, IPSET_EVENT_NAME);    
	SetEvent(eventhd);    
	CloseHandle(eventhd);   
	_endthreadex(0);    
	return -1;
}

unsigned __stdcall setIpAddr(void *pvParam)
{  
	CString strMsg;
	HANDLE eventhd;        
	_sleep(100);    

    eventhd = OpenEvent(EVENT_ALL_ACCESS, FALSE, IPSET_EVENT_NAME);    
	WaitForSingleObject(eventhd, INFINITE);
	CloseHandle(eventhd);

	Protectself("198.51.100.254",((SetUpStusDlg*)pvParam)->IpAddrArray);

	((SetUpStusDlg*)pvParam)->m_Progress.SetPos(100);

	((SetUpStusDlg*)pvParam)->IsContinue = TRUE;

	((SetUpStusDlg*)pvParam)->m_return.EnableWindow(TRUE);
	((SetUpStusDlg*)pvParam)->m_install.EnableWindow(TRUE);

	strMsg.LoadString(IDS_STRING_Sucees);
	SetUpInfoDlg infoDlg;
	infoDlg.DoModal(strMsg);
	_endthreadex(0);
	return 0;
}


void SetUpStusDlg::OnBnClickedOk2()
{
	if(m_nTimer){
		KillTimer(m_nTimer);
	}
	m_nTimer = SetTimer(1, 500, NULL);

	IsContinue = FALSE;

	CString strmsg;
	strmsg.LoadString(IDS_STRING_INSTALLING);
	SetDlgItemText(IDC_STATIC,strmsg);
	m_Progress.ShowWindow(TRUE);

	pThread = AfxBeginThread((AFX_THREADPROC)Thread1, 
                        (LPVOID)this,      // このクラスをパラメータとして渡しています
                        THREAD_PRIORITY_NORMAL,
                        0,
                        CREATE_SUSPENDED);//スタート時からサスペンド
	//pThread->SuspendThread();//スレッドのサスペンド
	pThread->ResumeThread();//スレッドのレジューム


}

//スレッドのメイン
UINT SetUpStusDlg::Thread1( LPVOID pvParam )
{
	SetThreadLocalSettings(((SetUpStusDlg*)pvParam)->Language,((SetUpStusDlg*)pvParam)->SubLanguage);

	CString strMsg;
	
	((SetUpStusDlg*)pvParam)->m_return.EnableWindow(FALSE);
	((SetUpStusDlg*)pvParam)->m_install.EnableWindow(FALSE);

	if(!HasLookAdapter("198.51.100.254"))
	{
		LPCTSTR BaseName = TEXT("BaseName"); 
		LPCTSTR Machine = nullptr;
		DWORD Flags =1;
		int argc = 2;
		TCHAR* argv[] ={TEXT("C:\\Windows\\Inf\\Netloop.inf"),TEXT("*MSLOOP")};
		int ret = InstallLkbkAdpt(BaseName,Machine,Flags,argc,argv);
		if(ret != 0)
		{
			((SetUpStusDlg*)pvParam)->m_return.EnableWindow(TRUE);
			((SetUpStusDlg*)pvParam)->m_install.EnableWindow(TRUE);

			strMsg.LoadString(IDS_STRING_Error);
			((SetUpStusDlg*)pvParam)->ShowWindow(SW_HIDE);
			SetUpInfoDlg infoDlg;
			infoDlg.DoModal(strMsg);
		}
	}
	((SetUpStusDlg*)pvParam)->m_Progress.SetPos(50);

	Protectself("198.51.100.254",((SetUpStusDlg*)pvParam)->IpAddrArray);

	((SetUpStusDlg*)pvParam)->m_Progress.SetPos(100);

	((SetUpStusDlg*)pvParam)->IsContinue = TRUE;

	((SetUpStusDlg*)pvParam)->m_return.EnableWindow(TRUE);
	((SetUpStusDlg*)pvParam)->m_install.EnableWindow(TRUE);

	strMsg.LoadString(IDS_STRING_Sucees);
	((SetUpStusDlg*)pvParam)->ShowWindow(SW_HIDE);
	SetUpInfoDlg infoDlg;
	infoDlg.DoModal(strMsg);
    return(0);
}


void SetUpStusDlg::OnBnClickedCancel()
{
	PostMessage(WM_CLOSE,0); 
	exit(0);
}
